//
//  JoinQuizVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/28.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "JoinQuizVC.h"
#import "JoinQuizCell.h"
#import "JoinQuizDetailVC.h"
#import "LotteryTypeView.h"
#import "MyVM.h"
#import "LotteryTypeModel.h"
@interface JoinQuizVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) LotteryTypeView *lotteryTypeView;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@end

@implementation JoinQuizVC{
    NSInteger   selectType;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"参与竞猜";
    [self.tableView reloadData];
    [self getData];
    // Do any additional setup after loading the view.
}

- (void)getData{
    WS(weakSelf);
    LotteryTypeModel *lModel=[BaseVM manage].lotteryArr[selectType];
    [[MyVM manage] getBetWithPId:lModel.Id andSuccess:^(id returnValue) {
        weakSelf.dataArr=returnValue;
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

#pragma mark -- UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    view.backgroundColor=DEFAULTBGCOLOR;
    return view;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JoinQuizCell *cell=[tableView dequeueReusableCellWithIdentifier:@"JoinQuizCell" forIndexPath:indexPath];
    cell.model=self.dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JoinQuizDetailVC *vc=[[JoinQuizDetailVC alloc]init];
    QuizLotteryModel *model=self.dataArr[indexPath.row];
    vc.title=model.Name;
    vc.lType=model.lType;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=DEFAULTBGCOLOR;
        [_tableView registerClass:[JoinQuizCell class] forCellReuseIdentifier:@"JoinQuizCell"];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.lotteryTypeView.mas_bottom);
        }];
    }
    return _tableView;
}

- (LotteryTypeView *)lotteryTypeView{
    if (!_lotteryTypeView) {
        WS(weakSelf);
        _lotteryTypeView=[[LotteryTypeView alloc]init];
        _lotteryTypeView.chaneBlock = ^(NSInteger row) {
            selectType=row;
            [weakSelf getData];
        };
        [self.view addSubview:_lotteryTypeView];
        [_lotteryTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(weakSelf.view);
            make.height.mas_equalTo(@40);
        }];
    }
    return _lotteryTypeView;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr =[[NSMutableArray alloc]init];
        
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
